import {Component, OnInit} from "@angular/core";
import {Page} from "../../../public/util/page";
import {PlatformService} from "../platform.service";
import {Enums, States} from '../../../public/setting/enums';
import {SettingUrl} from "../../../public/setting/setting_url";

@Component({
  selector: 'app-platform-account-list',
  templateUrl: './platform-account-list.component.html',
  styleUrls: ['./platform-account-list.component.scss']
})
export class PlatformAccountListComponent implements OnInit {
  public searchParams: any = {};//搜索参数
  public platformAccountList: Page = new Page(); //供应商
  public _loading: boolean = false;
  public routerLinks = SettingUrl.ROUTERLINK;//路由
  public states = Enums.platformAccountState;       // 平台账户状态
  public isSupper = Enums.yn;       // 是否是超级账户

  constructor(private platformService: PlatformService) {
  }

  ngOnInit() {
    this.queryPlatformAccountList()
  }

  /**
   * 查询platformAccount列表
   * @param curPage 当前页
   */
  queryPlatformAccountList(curPage?: number) {
    this._loading = true;
    if (curPage) this.platformAccountList.curPage = curPage;//当有页码时，查询该页数据
    this.platformAccountList.params = {
      curPage: this.platformAccountList.curPage, //目标页码
      pageSize: this.platformAccountList.pageSize, //每页条数
    };
    this.platformService.getPlatformAccountList(this.platformAccountList.params).then((res: Page) => {
      this._loading = false;
      this.platformAccountList = res;
    }).catch(err => {
      this._loading = false;
    })
  }

  /**
   * 修改禁用状态
   * @param code
   * @param event:boolean
   */
  modifyPlatformAccountState(code, event) {
    let state = event ? States.enable : States.disable;//转换为对应的枚举
    this.platformService.modifyPlatformAccountState(code, state).catch((res) => {
      this.queryPlatformAccountList();//由于switch的特殊性，因此在失败的时候刷新页面
    })
  }

  /**
   * 重置搜索
   */
  resetSearch() {
    this.searchParams = {};
    this.queryPlatformAccountList(1)
  }
}
